﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BikeBouncer.Site.Enums;
using Devv.Core.Utils;

namespace BikeBouncer.Site
{
	public partial class Ads_ascx : UserControl
	{
		#region Properties

		public AdSize Size { get; set; }
		public AdSource Source { get; set; }
		public String CssClass { get; set; }

		#endregion

		#region Main methods

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			if (Request.IsSecureConnection)
			{
				return;
			}

			if (Source == AdSource.AdSense)
			{
				if (Size == AdSize.Horizontal728)
				{
					PlaceAdsense728.Visible = true;
				}
			}
			else if (Source == AdSource.EvansCycles)
			{
				if (Size == AdSize.Horizontal728)
				{
					PlaceEvans728.Visible = true;
				}
			}
			else if (Source == AdSource.Amazon)
			{
				if (Size == AdSize.Horizontal400)
				{
					PlaceAmazonCarousel400.Visible = true;
				}
				else if (Size == AdSize.Vertical600)
				{
					PlaceAmazon160x600.Visible = true;
					SetAmazonKeywords(AmazonFrame160x600);
				}
				else if (Size == AdSize.Square468)
				{
					PlaceAmazon468x240.Visible = true;
					SetAmazonKeywords(AmazonFrame468x240);
				}
			}

			if (!String.IsNullOrEmpty(CssClass))
			{
				PanAds.CssClass = CssClass;
			}
		}

		private void SetAmazonKeywords(HtmlGenericControl frame)
		{
			String[] keywords = ConfigUtil.GetConfig("Amazon.Ads.SearchWords", "bike").Split("|".ToCharArray());
			String k = keywords[new Random().Next(0, keywords.Length)].Replace(" ", "%20");

			frame.Attributes["src"] = frame.Attributes["src"].Replace("#KEYWORDS#", k);
		}

		#endregion
	}
}